<?php
/**
 * Guard 辅助方法
 *
 * Author: Zeon
 * Date: 2017/11/14
 * Time: 17:19
 * Created by PhpStorm.
 */
namespace App\Services\Utils;



use App\Models\Customers\Customer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Tymon\JWTAuth\Contracts\JWTSubject;

class Guard
{
    const ADMIN_CUSTOMER = Customer::GUARD_NAME;

    /**
     * Delegate Gate Method On Auth::guard
     * @example
     *      Guard::matchId($admin_model, $operate_model)
     *
     * @param  string  $method
     * @param  mixed[2]  $parameters
     * @return mixed
     */
    public static function __callStatic($method, array $parameters)
    {
        list($admin, $arguments) = $parameters;

        return Gate::forUser($admin)->check($method, $arguments);
    }


    /**
     * @return Customer|null
     */
    public static function customer(): ?Customer
    {
        return Auth::guard(self::ADMIN_CUSTOMER)->user();
    }

    /**
     * @return bool
     * @throws \Exception
     */
    public static function hasJwtToken(): bool
    {
        /** @var Request $req */
        $req = request();

        return $req->get('token') && $req->bearerToken();
    }
}